// 登录钩子函数
import { auth } from '@/data/auth'
import type { ILoginInfo } from '../repo/user'
import { useUserStore } from '@/stores/user'
import { computed } from 'vue'

export function useAuth() {
  const store = useUserStore()
  //获取用户信息
  const user = computed(() => store.getUserInfo)
  //登录
  const loginFunc = async (data: ILoginInfo) => {
    const { token, userInfo } = await auth(data)
    //需要把token和用户信息保存在store中
    store.setUserInfo({ token, userInfo })
  }
  //登出
  const logoutFunc = () => {
    store.removeUserInfo()
  }
  return {
    loginFunc,
    logoutFunc,
    user
  }
}
